package com.markhsiu.framework.netty3.hander;

import java.nio.charset.Charset;
import java.util.concurrent.atomic.AtomicInteger;

import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelHandler;

public class ServerBufferHandler extends SimpleChannelHandler {

	private static AtomicInteger count = new AtomicInteger(0);

	/**
	 * 用户接受客户端发来的消息，在有客户端消息到达时触发
	 * 
	 */
	@Override
	public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
		 ChannelBuffer buffer = (ChannelBuffer) e.getMessage();
		 // 五位读取
		 while (buffer.readableBytes() >= 5) {
			 ChannelBuffer tempBuffer = buffer.readBytes(5);
			 System.out.println(tempBuffer.toString(Charset.defaultCharset()));
		 }
		 // 读取剩下的信息
		 System.out.println(buffer.toString(Charset.defaultCharset()));

//		int c = count.getAndIncrement();
//		System.out.println(" server ..... "+c);

	}

	public static int getCount() {
		return count.get();
	}
}
